﻿using System;
using System.Collections.Generic;
using System.Web;

using System.Data;
using HZ.Supervise.IBLL;
using HZ.Supervise.BLL;
using HZ.Supervise.Model;

using HZ.Supervise.Model.Enum;

/// <summary>
///UserRuleCache 的摘要说明
/// </summary>
public class UserRuleCache
{
    private const string RuleCache = "UserRuleCache"; 

    public static IList<HZ.Supervise.Model.OperatingRule> UserRule
    {
        get
        {
            IList<HZ.Supervise.Model.OperatingRule> _UserRule = null;
            if (HttpContext.Current.Session[RuleCache] == null)
            { 
                IBLL_User userCtrl = new BLL_User();
                _UserRule = userCtrl.GetUserOperatingRule(CurrentAccount.UserID);
                HttpContext.Current.Session[RuleCache] = _UserRule;
            }

            return HttpContext.Current.Session[RuleCache] as IList<HZ.Supervise.Model.OperatingRule>; 
        }
        set { HttpContext.Current.Session[RuleCache] = null; }
    }

    /// <summary>
    /// 判断当前用户是否有指定操作权限
    /// </summary>
    /// <param name="module"></param>
    /// <param name="operateType"></param>
    /// <returns></returns>
    public static bool CheckRule(HZ.Supervise.Model.Enum.Module module, HZ.Supervise.Model.Enum.OperateType operateType)
    {
        DataTable userRuleTable = HZ.Data.ModelConvert.GetDataTableFromModel(UserRule);
        DataView dv = new DataView(userRuleTable);

        //设置过滤条件
        dv.RowFilter = "Module='" + module.ToString() + "' AND OperatingRuleName ='" + operateType.ToString() + "'";

        return dv.Count > 0;
    } 

    /// <summary>
    /// 页面级权限判断
    /// </summary>
    /// <param name="strModuleName"></param>
    /// <returns></returns>
    public static bool ValidatePageRight(string strModuleNo, string strOperateType)
    {
        bool isHaveRight = true;
        if (strModuleNo != "")
        {
            isHaveRight = UserRuleCache.CheckRule(EnumParse.GetModule(strModuleNo).Value, EnumParse.GetOperateType(strOperateType).Value);
        }
        return isHaveRight;
    }
     

}
